package com.cupdata.marathon.client;

import com.cupdata.marathon.definition.RspCd;
import com.cupdata.marathon.param.membercheck.MemCheckRequest;
import com.cupdata.marathon.param.membercheck.MemCheckResponse;
import com.cupdata.marathon.utils.GsonUtils;
import com.unionpay.magpie.client.ServiceRegistry;
import com.unionpay.magpie.remoting.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class MemberCheckService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String MEMBER_CHECK_SERVICE = "member_check_service";

    public MemCheckResponse call(MemCheckRequest memCheckRequest) {
        try {
            byte[] resultBytes = ServiceRegistry.getService(MEMBER_CHECK_SERVICE).call(GsonUtils.gson.toJson(memCheckRequest).getBytes());
            String res = new String(resultBytes);
            logger.info("Received message: {}", res);
            return GsonUtils.gson.fromJson(new String(resultBytes), MemCheckResponse.class);
        } catch (RemotingException e) {
            logger.error("Magpie fail: {}", e.getMessage());
            MemCheckResponse response = new MemCheckResponse();
            response.setRspCd(RspCd.FAIL.getCode());
            response.setRspMsg("请求超时");
            return response;
        }
    }
}
